<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>HTML5预加载、预渲染 | Jason&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="HTML5预加载、预渲染">
<meta property="og:type" content="article">
<meta property="og:title" content="HTML5预加载、预渲染">
<meta property="og:url" content="https://xianguoji.gitee.io/myblog/2016/10/04/2016-10-4-HTML5%E9%A2%84%E5%8A%A0%E8%BD%BD%E3%80%81%E9%A2%84%E6%B8%B2%E6%9F%93/index.html">
<meta property="og:site_name" content="Jason&#39;s Blog">
<meta property="og:description" content="HTML5预加载、预渲染">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2016-10-03T16:00:00.000Z">
<meta property="article:modified_time" content="2020-08-12T06:46:17.046Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="性能优化">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/myblog/" title="Jason&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/myblog/css/style.css">

<meta name="generator" content="Hexo 5.0.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/myblog/" id="logo">人与人的悲欢并不相通，孤独是生命的本质。</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/myblog/" id="subtitle">欢迎来到我的个人网站</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/myblog/">Home</a>
        
          <a class="main-nav-link" href="/myblog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/myblog/" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://xianguoji.gitee.io/myblog"></form>
      </div>
    </div>
  </div>
</header>
<style>
  #logo{
    text-shadow:0 0 5px #000;
    /* font-weight: bold; */
  }
  #subtitle-wrap{
    margin-top: 50px;
  }
</style>
      <div class="outer">
        <section id="main"><article id="post-2016-10-4-HTML5预加载、预渲染" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/myblog/2016/10/04/2016-10-4-HTML5%E9%A2%84%E5%8A%A0%E8%BD%BD%E3%80%81%E9%A2%84%E6%B8%B2%E6%9F%93/" class="article-date">
  <time datetime="2016-10-03T16:00:00.000Z" itemprop="datePublished">2016-10-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      HTML5预加载、预渲染
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>prefetch 即预加载，在用户需要前我们就将所需的资源加载完毕。</p>
<p>有了浏览器缓存，为何还需要预加载？</p>
<p>用户可能是第一次访问网站，此时还无缓存</p>
<p>用户可能清空了缓存</p>
<p>缓存可能已经过期，资源将重新加载</p>
<p>用户访问的缓存文件可能不是最新的，需要重新加载</p>
<p>Chrome 的预加载技术</p>
<p>现在的 chrome 聪明到根据你的浏览记录，预测到你可能访问或搜索哪些网站，在你打开网站之前就加载好了一些资源了。</p>
<p>举个栗子，当你在搜索框输入 “amaz” 时，它猜测到你可能要访问 amazon.com，可能就帮你加载了这个网站的一些资源。</p>
<p>如果这个预测算法精准的话，就能大大地提高用户的浏览体验了。</p>
<p>DNS prefetch<br>我们知道，当我们访问一个网站如 <a target="_blank" rel="noopener" href="http://www.amazon.com/">www.amazon.com</a> 时，需要将这个域名先转化为对应的 IP 地址，这是一个非常耗时的过程。</p>
<p>DNS prefetch 分析这个页面需要的资源所在的域名，浏览器空闲时提前将这些域名转化为 IP 地址，真正请求资源时就避免了上述这个过程的时间。</p>
<pre><code>&lt;meta http-equiv=&#39;x-dns-prefetch-control&#39; content=&#39;on&#39;&gt;
&lt;link rel=&#39;dns-prefetch&#39; href=&#39;http://g-ecx.images-amazon.com&#39;&gt;
&lt;link rel=&#39;dns-prefetch&#39; href=&#39;http://z-ecx.images-amazon.com&#39;&gt;
&lt;link rel=&#39;dns-prefetch&#39; href=&#39;http://ecx.images-amazon.com&#39;&gt;
&lt;link rel=&#39;dns-prefetch&#39; href=&#39;http://completion.amazon.com&#39;&gt;
&lt;link rel=&#39;dns-prefetch&#39; href=&#39;http://fls-na.amazon.com&#39;&gt;</code></pre>
<p>应用场景1：我们的资源存在在不同的 CDN 中，那提前声明好这些资源的域名，就可以节省请求发生时产生的域名解析的时间。</p>
<p>应用场景2：如果我们知道用户接下来的操作一定会发起一起资源的请求，那就可以将这个资源进行 DNS-Prefetch，加强用户体验。</p>
<p>Resource prefetch</p>
<p>在 Chrome 下，我们可以用 link标签声明特定文件的预加载：</p>
<pre><code>&lt;link rel=&#39;subresource&#39; href=&#39;critical.js&#39;&gt;
&lt;link rel=&#39;subresource&#39; href=&#39;main.css&#39;&gt;
&lt;link rel=&#39;prefetch&#39; href=&#39;secondary.js&#39;&gt;</code></pre>
<p>在 Firefox 中或用 meta 标签声明：</p>
<pre><code>&lt;meta http-equiv=&quot;Link&quot; content=&quot;&lt;critical.js&gt;; rel=prefetch&quot;&gt;</code></pre>
<p>rel=’subresource’ 表示当前页面必须加载的资源，应该放到页面最顶端先加载，有最高的优先级。</p>
<p>rel=’prefetch’ 表示当 subresource 所有资源都加载完后，开始预加载这里指定的资源，有最低的优先级。</p>
<p>注意：只有可缓存的资源才进行预加载，否则浪费资源！</p>
<p>Pre render</p>
<p>前面说到的预解析DNS、预加载资源已经够强悍了有木有，可还有更厉害的预渲染（Pre-rendering）！</p>
<p>预渲染意味着我们提前加载好用户即将访问的下一个页面，否则进行预渲染这个页面将浪费资源，慎用！</p>
<pre><code>&lt;link rel=&#39;prerender&#39; href=&#39;http://www.pagetoprerender.com&#39;&gt;</code></pre>
<p>rel=’prerender’ 表示浏览器会帮我们渲染但隐藏指定的页面，一旦我们访问这个页面，则秒开了！</p>
<p>在 Firefox 中或用 rel=’next’ 来声明</p>
<pre><code>&lt;link rel=&quot;next&quot; href=&quot;http://www.pagetoprerender.com&quot;&gt;</code></pre>
<p>不是所有的资源都可以预加载</p>
<p>当资源为以下列表中的资源时，将阻止预渲染操作：</p>
<p>URL 中包含下载资源</p>
<p>页面中包含音频、视频</p>
<p>POST、PUT 和 DELETE 操作的 ajax 请求</p>
<p>HTTP 认证(Authentication)</p>
<p>HTTPS 页面</p>
<p>含恶意软件的页面</p>
<p>弹窗页面</p>
<p>占用资源很多的页面</p>
<p>打开了 chrome developer tools 开发工具</p>
<p>手动触发预渲染操作</p>
<p>在 head 中强势插入 link[rel=’prerender’] 即可：</p>
<pre><code>var hint =document.createElement(&quot;link&quot;)
hint.setAttribute(“rel”,”prerender”)
hint.setAttribute(“href”,”next-page.html”)
document.getElementsByTagName(“head”)[0].appendChild(hint)</code></pre>
<p>兼容性</p>
<p>这么好用的特性，当然要考虑各浏览器的兼容程度了(哭：</p>
<p>IE9 支持 DNS pre-fetching 但管它叫 prefetch。</p>
<p>IE10+ 中 dns-prefetch 和 prefetch 是等价的。</p>
<p>其他方面的测试，目前还没有很好的方案，暂且只能通过查看浏览器是否缓存来测试。</p>
<p>在 Chrome 中打开了 chrome developer tools 开发工具会阻止页面的预渲染，所以我们看不到这个过程，但可以在 chrome://cache/ 或 chrome://net-internals/#prerender 中查看。</p>
<p>Firefox 可以在 about:cache 中查看。</p>
<p>警告</p>
<p>这些特定还是实验性质的，将来可能改变。</p>
<p>权利越大，责任越大，不要滥用！</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://xianguoji.gitee.io/myblog/2016/10/04/2016-10-4-HTML5%E9%A2%84%E5%8A%A0%E8%BD%BD%E3%80%81%E9%A2%84%E6%B8%B2%E6%9F%93/" data-id="ckds6fxa30008k8qcc6dydbxd" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myblog/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="tag">性能优化</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/myblog/2016/10/17/2016-10-17-rem%E9%80%82%E9%85%8D-for-iphone/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          rem适配-for-iphone
        
      </div>
    </a>
  
  
    <a href="/myblog/2016/09/20/2016-09-20-%E5%A5%BD%E7%94%A8%E7%9A%84%E5%87%A0%E4%B8%AA%E5%89%8D%E7%AB%AFwebUI%E6%A1%86%E6%9E%B6/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">好用的几个前端webUI框架</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/javascript/" rel="tag">javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/rem/" rel="tag">rem</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E5%85%B6%E4%BB%96/" rel="tag">其他</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E5%8D%9A%E5%AE%A2/" rel="tag">博客</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="tag">性能优化</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E6%A1%86%E6%9E%B6/" rel="tag">框架</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E7%A7%BB%E5%8A%A8%E7%AB%AF/" rel="tag">移动端</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myblog/tags/%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82/" rel="tag">跨域请求</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/myblog/tags/javascript/" style="font-size: 20px;">javascript</a> <a href="/myblog/tags/js/" style="font-size: 10px;">js</a> <a href="/myblog/tags/rem/" style="font-size: 16.67px;">rem</a> <a href="/myblog/tags/%E5%85%B6%E4%BB%96/" style="font-size: 10px;">其他</a> <a href="/myblog/tags/%E5%8D%9A%E5%AE%A2/" style="font-size: 13.33px;">博客</a> <a href="/myblog/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" style="font-size: 13.33px;">性能优化</a> <a href="/myblog/tags/%E6%A1%86%E6%9E%B6/" style="font-size: 10px;">框架</a> <a href="/myblog/tags/%E7%A7%BB%E5%8A%A8%E7%AB%AF/" style="font-size: 13.33px;">移动端</a> <a href="/myblog/tags/%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82/" style="font-size: 10px;">跨域请求</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2020/08/">August 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/06/">June 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/05/">May 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/04/">April 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/03/">March 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/02/">February 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2017/01/">January 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/10/">October 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/09/">September 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2016/08/">August 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2015/08/">August 2015</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/myblog/2020/08/13/2020-08-13%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">(no title)</a>
          </li>
        
          <li>
            <a href="/myblog/2017/06/18/2017-06-18-%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E4%B8%80/">前端性能优化一</a>
          </li>
        
          <li>
            <a href="/myblog/2017/05/15/2017-05-15-%E8%B0%83%E7%94%A8%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E6%8F%92%E4%BB%B6-%E6%9B%B4%E6%96%B0/">调用百度地图插件-更新</a>
          </li>
        
          <li>
            <a href="/myblog/2017/04/23/2017-04-23-vue%E6%90%AD%E5%BB%BA%E9%A1%B9%E7%9B%AE/">vue搭建项目</a>
          </li>
        
          <li>
            <a href="/myblog/2017/04/21/2017-04-21-%E8%B0%83%E7%94%A8%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E6%8F%92%E4%BB%B6/">调用百度地图插件</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 Jason<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/myblog/" class="mobile-nav-link">Home</a>
  
    <a href="/myblog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/myblog/fancybox/jquery.fancybox.css">

  
<script src="/myblog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/myblog/js/script.js"></script>




  </div>
</body>
</html>